/* 
 * Copyright [2018] [Alex/libo(liboware@gmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.infodb.file.web.directory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hyts.infodb.enums.ModuleNameEnum;
import com.hyts.infodb.enums.SystemConfiguration.MessageConfiguration;
import com.hyts.infodb.file.bean.directory.DirectoryBO;
import com.hyts.infodb.file.bean.directory.DirectoryDTO;
import com.hyts.infodb.file.bean.directory.DirectoryVO;
import com.hyts.infodb.file.service.directory.DirectoryService;

/** 
 * @author LiBo/Alex
 * @version V1.0 
 */
@RestController
@RequestMapping("/directory")
public class DirectoryController {
    
    @Autowired
    DirectoryService service;
    
    /**
     * <b>查询基本信息</b>
     * @param id
     * @return
     * @exception
     */
    @RequestMapping("/load/{id}")
    public DirectoryDTO loadDirectoryInfo(@PathVariable("id") Long id) {
        DirectoryDTO directoryDTO = new DirectoryDTO();
        DirectoryBO bo = new DirectoryBO();
        bo.setId(id);
        DirectoryVO vo = service.loadDirectory(bo);
        if(vo == null || vo.getContent() == null) {
            return (DirectoryDTO) directoryDTO.failure(MessageConfiguration.LOAD,ModuleNameEnum.DIRECTORY_MODULE.getModuleName());
        }else {
            return (DirectoryDTO) directoryDTO.success(MessageConfiguration.LOAD,ModuleNameEnum.DIRECTORY_MODULE.getModuleName(),vo.getContent());
        }
    }
    
    /**
     * <b>保存基本信息</b>
     * @param file
     * @return
     * @exception
     */
    @RequestMapping("/save")
    public DirectoryDTO saveDirectoryInfo(@RequestBody DirectoryBO directory) {
        DirectoryDTO directoryDTO = new DirectoryDTO();
        boolean result = service.saveDirectory(directory);
        if(result) {
            return (DirectoryDTO) directoryDTO.success(MessageConfiguration.SAVE,ModuleNameEnum.DIRECTORY_MODULE.getModuleName());
        }else {
            return (DirectoryDTO) directoryDTO.failure(MessageConfiguration.SAVE,ModuleNameEnum.DIRECTORY_MODULE.getModuleName());
        }
    }
    
    /**
     * <b>删除基本信息</b>
     * @param id
     * @return
     * @exception
     */
    @RequestMapping("/delete/{id}")
    public DirectoryDTO deleteDirectoryInfo(@PathVariable("id") Long id) {
        DirectoryDTO directoryDTO = new DirectoryDTO();
        DirectoryBO bo = new DirectoryBO();
        bo.setId(id);
        boolean result = service.deleteDirectory(bo);
        if(result) {
            return (DirectoryDTO) directoryDTO.success(MessageConfiguration.REMOVE,ModuleNameEnum.DIRECTORY_MODULE.getModuleName());
        }else {
            return (DirectoryDTO) directoryDTO.failure(MessageConfiguration.REMOVE,ModuleNameEnum.DIRECTORY_MODULE.getModuleName());
        }
    }
    
    /**  
     * @param directory
     * @return
     * @exception
     */ 
    @RequestMapping("/find")
    public DirectoryDTO findDirectoryList(@RequestBody DirectoryBO directory) {
        DirectoryVO vo = service.findDirectoryList(directory);
        DirectoryDTO directoryDTO = new DirectoryDTO();
        if(vo == null) {
            return (DirectoryDTO) directoryDTO.failure(MessageConfiguration.QUERY,ModuleNameEnum.DIRECTORY_MODULE.getModuleName());
        }else {
            return (DirectoryDTO) directoryDTO.success(MessageConfiguration.QUERY,ModuleNameEnum.DIRECTORY_MODULE.getModuleName(),vo.getContents());
        }
    }
    
}
